'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;
exports.main = async (event, context) => {
	try {
		let space_id = 'tcb-pfeyfxdttpsw2jy-8cua388a9814'
		let user_id = 'e91541bb649a8262000113db70fe74ad'
		let service_user_id = 'e91541bb649a8262000113db70fe74ad'
		let regsterDatas = await db.collection('wmzmall-register-logs').where({
			auth: 0
		}).get()
		regsterDatas = regsterDatas.data
		console.log(regsterDatas.length + "条数据待认证")
		for (var i in regsterDatas) {
			let tDb = uniCloud.database({
				provider: 'tencent',
				spaceId: space_id
			})
			let regsterData = regsterDatas[i]
			let curUser = await tDb.collection('uni-id-users').doc(user_id).get()
			if (curUser.data.length === 0) {
				return result_error('账号异常')
			}
			curUser = curUser.data[0]
			let serviceProviders = await tDb.collection('wmz-service-providers').where({
				user_id: service_user_id
			}).get()
			serviceProviders = serviceProviders.data[0]
			let service_id = serviceProviders._id
			let alipayData = serviceProviders.alipay
			let qrcode_path = serviceProviders.qrcode_path

			let userFId = await tDb.collection('wmz-users-f-id').where({
				user_id: user_id
			}).get()
			userFId = userFId.data[0]
			let role = [
				"wmz_enterprise",
				"wmz_digitalWear"
			]
			space_id = 'tcb-72aw6re2d2sk68w-5ckq803ab518'

			let template_id = alipayData.ent_template_id
			let userData = {
				"mobile": regsterData.username,
				"mobile_confirmed": 1,
				"nickname": regsterData.name,
				"password": "3b91bb6d75cc2ad6598bd206af5acfe8928d0a9b",
				"role": role,
				"status": 0,
				"tags": [],
				"username": regsterData.username
			}
			const transaction = await tDb.startTransaction()
			let res = await transaction.collection('uni-id-users').add(userData)
			console.log("增加服务助手用户成功")
			if (res.id) {
				let appletData = {
					"auto_deliver": true,
					shop_name: regsterData.shop_name,
					user_id: res.id,
					user_name: regsterData.username,
					f_id: userFId._id,
					creator_id: curUser._id,
					creator_name: curUser.mobile,
					creator_mobile_name: curUser.nickname + "(" + curUser.mobile + ")",
					create_time: new Date().getTime(),
					type: '数字穿戴零售',
					self_config: true,
					alipay: {
						isv_pid: alipayData.pid,
						template_id: template_id,
						app_id: alipayData.app_id,
						app_private_key: alipayData.app_private_key,
						share_account: serviceProviders.alipay.share_account,
						share_name: serviceProviders.alipay.share_name
					},
					space_id: space_id,
					service_id: service_id,
					qrcode_path: qrcode_path
				}
				res = await transaction.collection('wmz-applet-info').add(appletData)
				console.log("增加服务助手应用信息成功")
				await transaction.collection('wmz-merchant-info').add({
					applet_name: regsterData.shop_name,
					address: regsterData.shop_adress,
					f_id: userFId._id,
					creator_id: curUser._id,
					creator_name: curUser.mobile,
					creator_mobile_name: curUser.nickname + "(" + curUser.mobile + ")",
					create_time: new Date().getTime(),
					app_id: res.id,
					user_name:regsterData.username
				})
				console.log("增加服务助手商户信息成功")
				await transaction.commit()
				let tDb = uniCloud.database({
					provider: 'tencent',
					spaceId: space_id
				})
				userData._id = appletData.user_id
				userData.app_id = res.id
				userData.shop_id = res.id
				userData.space_id = space_id
				await tDb.collection('uni-id-users').add(userData)
				console.log("增加商域宝用户成功")
				appletData._id = userData.app_id
				await tDb.collection('wmz-applet-info').add(appletData)
				console.log("增加商域宝应用信息户成功")
				await db.collection('wmz-applet-info').add(appletData)
				console.log("增加智链穿戴应用信息成功")
				await db.collection('uni-id-users').where({
					username: regsterData.username
				}).update({
					app_id: userData.app_id,
					shop_id: userData.app_id
				})
				console.log("修改智链穿戴用户成功")
				await db.collection('wmzmall-member').where({
					member_mobile: regsterData.username
				}).update({
					app_id: userData.app_id,
					shop_id: userData.app_id
				})
				console.log("修改智链穿戴会员成功")
				await db.collection('wmzmall-register-logs').doc(regsterData._id).update({
					auth: 1
				})
				console.log("修改认证状态户成功")
			}
		}
	} catch (error) {
		await db.collection('wmzmall-register-logs').doc(regsterData._id).update({
			auth: 2
		})
	}
};